package com.example.serverdemo.netty.server;

import com.example.serverdemo.netty.initializer.DeviceCommunicationInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**

 * @author  XiaoSa

 * @create  2020/9/30 0030 15:42

 * @desc    netty服务器启动监听

 **/
@Component
public class DeviceCommunicationServer {

    public void  start(InetSocketAddress socketAddress){
        //new 一个主线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        //new 一个攻错线程组
        EventLoopGroup workGroup = new NioEventLoopGroup(4);
        ServerBootstrap bootstrap = new ServerBootstrap()
                .group(bossGroup,workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new DeviceCommunicationInitializer())
                .localAddress(socketAddress)
                //设置队列大小
                .option(ChannelOption.SO_BACKLOG,1024)
                //两小时内没有数据的通信时，TCP会自动发送一个活动探测数据报文
                .childOption(ChannelOption.SO_KEEPALIVE,true);
        //绑定端口，开始接受进来的连接
        try {
            ChannelFuture future = bootstrap.bind(socketAddress).sync();
            System.out.println("服务器启动开始监听端口：" + socketAddress.getPort());
//            future.channel().closeFuture().sync();
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            //关闭主线程组
//            bossGroup.shutdownGracefully();
            //关闭工作线程组
//            workGroup.shutdownGracefully();
        }
    }
}
